package com.my.springbootlogback.controller;

import com.my.common.result.R;
import com.my.springbootlogback.entity.Log;
import com.my.springbootlogback.enums.LogTypeEnum;
import com.my.springbootlogback.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/log")
@Slf4j
public class LogController {

    @Autowired
    private LogService logService;


    @PostMapping("/save")
    public R<Log> save(@RequestBody Log entity) {
        log.info("LogController.save() -- start ------- data={}", entity);
        logService.save(entity);
        log.info("LogController.save() -- end -------");
        return R.success(entity);
    }

    @GetMapping("/query/{id}")
    public R<Log> query(@PathVariable Long id) {
        log.info("LogController.query() -- start ------- id={}", id);
        Log data = logService.getById(id);
        if (Objects.isNull(data)) {
            log.error("LogController.query() -- error, this {} is not found!", id);
        }
        data.setTypeName(LogTypeEnum.getEnumByCode(data.getType()).getName());
        log.info("LogController.query() -- end ------- data={}", data);
        return R.success(data);
    }

}
